home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / lisp / hyperbole / htz.el < prev    next >
Encoding:
Text File  |  1995-07-08  |  14.8 KB  |  395 lines

  1. ;;!emacs
  2. ;;
  3. ;; FILE:         htz.el
  4. ;; SUMMARY:      Timezone-based time and date support for Hyperbole.
  5. ;; USAGE:        GNU Emacs Lisp Library
  6. ;; KEYWORDS:     calendar, hypermedia
  7. ;;
  8. ;; AUTHOR:       Masanobu UMEDA             / Bob Weiner
  9. ;; ORG:          Fujitsu Laboratories LTD.  / Brown U.
  10. ;;
  11. ;; ORIG-DATE:    14-Oct-91 at 07:22:08
  12. ;; LAST-MOD:      7-Jul-95 at 14:59:51 by Bob Weiner
  13. ;;
  14. ;; This file is part of Hyperbole.
  15. ;; Available for use and distribution under the same terms as GNU Emacs.
  16. ;;
  17. ;; Copyright (C) 1991-1995, Free Software Foundation, Inc.
  18. ;; Developed with support from Motorola Inc.
  19. ;;
  20. ;; Adapted from Timezone package for GNU Emacs
  21. ;; Copyright(C) 1990 Masanobu UMEDA (umerin@mse.kyutech.ac.jp)
  22. ;; $Header: /pro/info/cvs/hypb/htz.el,v 1.2 1992/05/14 10:12:09 rsw Exp $
  23. ;;
  24. ;; DESCRIPTION:  
  25. ;;
  26. ;; All date parsing functions accept the output of any other parsing
  27. ;; function as input, so one can convert to a sortable date format, do a
  28. ;; compare and the display the result in a user selected format.
  29. ;; All date formats use a 4-digit year, so there are no problems around the
  30. ;; turn of the century.
  31. ;;
  32. ;; Hyperbole uses this package to normalize all worldwide date times to
  33. ;; Greenwich Mean Time, so that Hyperbole buttons created by users in
  34. ;; different timezones will sort by time properly.
  35. ;;
  36. ;; DESCRIP-END.
  37.  
  38. ;;; ************************************************************************
  39. ;;; Other required Elisp libraries
  40. ;;; ************************************************************************
  41.  
  42. (require 'hypb)
  43.  
  44. ;;; ************************************************************************
  45. ;;; Public functions
  46. ;;; ************************************************************************
  47.  
  48. (defun htz:date-arpa (&optional date local timezone)
  49.   "Convert optional DATE or current date to an arpanet standard date.
  50. Optional 1st argument LOCAL specifies the default local timezone of the DATE.
  51. Optional 2nd argument TIMEZONE specifies a timezone to be represented in."
  52.   (or (vectorp date)
  53.       (setq date (htz:date-parse (or date (current-time-string)))))
  54.   (let* ((year   (string-to-int (aref date 0)))
  55.      (month  (string-to-int (aref date 1)))
  56.      (day    (string-to-int (aref date 2)))
  57.      (time   (htz:time-parse (aref date 3)))
  58.      (hour   (string-to-int (aref time 0)))
  59.      (minute (string-to-int (aref time 1)))
  60.      (second (string-to-int (aref time 2)))
  61.      (local  (or (aref date 4) local htz:local)) ;Use original if defined
  62.      (timezone (or timezone local))
  63.      (diff   (- (htz:zone-to-hour timezone)
  64.             (htz:zone-to-hour local)))
  65.      (new    (htz:time-fix year month day
  66.                     (+ hour diff) minute second)))
  67.     (htz:date-make-arpa (aref new 0) (aref new 1) (aref new 2)
  68.                  (htz:time-make-string
  69.                   (aref new 3) (aref new 4) (aref new 5))
  70.                  timezone)))
  71.  
  72. (defun htz:date-parse (date &optional parsed-current-date)
  73.   "Parse DATE string and return a vector [year month day time timezone].
  74. 19 is prepended to year if necessary.  Timezone in DATE is optional, it
  75. defaults to the value of htz:local.
  76.  
  77. Recognizes the following styles:
  78.  (1) 14 Apr 89 03:20[:12] [GMT]
  79.  (2) Fri, 17 Mar [19]89 4:01[:33] [GMT]
  80.  (3) Mon Jan 16 16:12[:37] [GMT] 1989
  81.  (4) 19911014:07:51:08 or 1991101407:51:08  'sortable date'
  82.  (5) Mar 29 14:00    'ls -l date'  requires 'parsed-current-date' arg
  83.  (6) Mar  7  1994    'ls -l date'  requires 'parsed-current-date' arg"
  84.   (let ((date (or date ""))
  85.     (year nil)
  86.     (month nil)
  87.     (day nil)
  88.     (time nil)
  89.     (zone nil)) ; This may be nil.
  90.     (cond ((string-match
  91. "\\([0-9][0-9][0-9][0-9]\\)\\([0-1][0-9]\\)\\([0-3][0-9]\\):?\\([0-2][0-9]:[0-5][0-9:]+\\)[ ]*\\'" date)
  92.        ;; Style (4)
  93.        (setq year 1 month 2 day 3 time 4 zone nil))
  94.       ((string-match
  95. "\\([0-9]+\\) \\([^ ,]+\\) \\([0-9]+\\) \\([0-9]+:[0-9:]+\\)[ ]*\\'" date)
  96.        ;; Styles: (1) and (2) without timezone
  97.        (setq year 3 month 2 day 1 time 4 zone nil))
  98.       ((string-match
  99. "\\([0-9]+\\) \\([^ ,]+\\) \\([0-9]+\\) \\([0-9]+:[0-9:]+\\)[ ]*\\([-+a-zA-Z0-9]+\\)" date)
  100.        ;; Styles: (1) and (2) with timezone and buggy timezone
  101.        (setq year 3 month 2 day 1 time 4 zone 5))
  102.       ((string-match
  103. "\\([^ ,]+\\) +\\([0-9]+\\) \\([0-9]+:[0-9:]+\\) \\([0-9]+\\)" date)
  104.        ;; Styles: (3) without timezone
  105.        (setq year 4 month 1 day 2 time 3 zone nil))
  106.       ((string-match
  107. "\\([^ ,]+\\) +\\([0-9]+\\) \\([0-9]+:[0-9:]+\\) \\([-+a-zA-Z0-9]+\\) \\([0-9]+\\)" date)
  108.        ;; Styles: (3) with timezone
  109.        (setq year 5 month 1 day 2 time 3 zone 4))
  110.       ((string-match "^\\([^ ,]+\\) +\\([0-9]+\\) +\\([0-9]+:[0-9:]+\\)$" date)
  111.        ;; Style: (5)
  112.        (setq year nil month 1 day 2 time 3 zone nil))
  113.       ((string-match
  114.         "^\\([^ ,]+\\) +\\([0-9]+\\) +\\([0-9][0-9][0-9][0-9]\\)$" date)
  115.        ;; Style: (6)
  116.        (setq year 3 month 1 day 2 time nil zone nil))
  117.       (t (error "(htz:date-parse): Invalid date format: '%s'" date)))
  118.     (if year
  119.       (setq year
  120.         (substring date (match-beginning year) (match-end year))
  121.         year (if (/= (length year) 2) year
  122.                (let* ((yr (substring (current-time-string) -4))
  123.                   (curr-yr (substring yr 2))
  124.                   (century (substring yr 0 2)))
  125.              (concat (if (string< curr-yr yr)
  126.                      (format "%02d"
  127.                          (1- (string-to-int century)))
  128.                    century)
  129.                  year))))
  130.       (setq year (if (vectorp parsed-current-date)
  131.              (aref parsed-current-date 0)
  132.            "0")))
  133.     (if month
  134.     (setq month (substring date
  135.                    (match-beginning month) (match-end month))
  136.           month (if (/= (string-to-int month) 0) month
  137.               (int-to-string
  138.                (cdr (assoc (upcase month) htz:months-assoc)))))
  139.       (setq month (if (vectorp parsed-current-date)
  140.               (aref parsed-current-date 1)
  141.             "0")))
  142.     (if day
  143.     (setq day (substring date (match-beginning day) (match-end day)))
  144.       (setq day (if (vectorp parsed-current-date)
  145.             (aref parsed-current-date 2)
  146.           "0")))
  147.     (if time
  148.     (setq time (substring date (match-beginning time) (match-end time)))
  149.       (setq time (if (vectorp parsed-current-date)
  150.              (aref parsed-current-date 3))))
  151.     (if zone
  152.     (setq zone (substring date (match-beginning zone) (match-end zone)))
  153.       (setq zone (if (vectorp parsed-current-date)
  154.              (aref parsed-current-date 4)
  155.            htz:local)))
  156.     ;; Return a vector.
  157.     (vector year month day time zone)))
  158.  
  159. (defun htz:date-sortable (&optional date local timezone)
  160.   "Convert optional DATE or current date to a sortable date string.
  161. Optional 1st argument LOCAL specifies the local timezone of the DATE.
  162. Optional 2nd argument TIMEZONE specifies an output timezone to use."
  163.   (or (vectorp date)
  164.       (setq date (htz:date-parse (or date (current-time-string)))))
  165.   (let* ((year   (string-to-int (aref date 0)))
  166.      (month  (string-to-int (aref date 1)))
  167.      (day    (string-to-int (aref date 2)))
  168.      (time   (htz:time-parse (aref date 3)))
  169.      (hour   (string-to-int (aref time 0)))
  170.      (minute (string-to-int (aref time 1)))
  171.      (second (string-to-int (aref time 2)))
  172.      (local  (or (aref date 4) local htz:local)) ;Use original if defined
  173.      (timezone (or timezone local))
  174.      (diff   (- (htz:zone-to-hour timezone)
  175.             (htz:zone-to-hour local)))
  176.      (new    (htz:time-fix year month day
  177.                     (+ hour diff) minute second)))
  178.     (htz:date-make-sortable (aref new 0) (aref new 1) (aref new 2)
  179.                  (htz:time-make-string
  180.                   (aref new 3) (aref new 4) (aref new 5)))))
  181.  
  182. ;;;
  183. ;;; Parsers and Constructors of Date and Time
  184. ;;;
  185.  
  186. (defun htz:date-sortable-gmt (&optional date local)
  187.   "Convert optional DATE or current date  to a sortable date string in Greenwich Mean Time.
  188. Optional argument LOCAL specifies the local timezone of the DATE."
  189.   (htz:date-sortable date local "GMT"))
  190.  
  191. (defun htz:date-unix (&optional date local timezone)
  192.   "Convert DATE or current date to a unix standard date.
  193. Optional 1st argument LOCAL specifies the local timezone of the DATE (default
  194. is the timezone embedded in the date or if there is none, then the value of
  195. the variable, htz:local).  Optional 2nd argument TIMEZONE specifies the
  196. timezone in which the date is returned; it defaults to the value of
  197. htz:local."
  198.   (or (vectorp date)
  199.       (setq date (htz:date-parse (or date (current-time-string)))))
  200.   (or local (setq local (or (aref date 4) htz:local)))
  201.   (let* ((year   (string-to-int (aref date 0)))
  202.      (month  (string-to-int (aref date 1)))
  203.      (day    (string-to-int (aref date 2)))
  204.      (time   (htz:time-parse (aref date 3)))
  205.      (hour   (string-to-int (aref time 0)))
  206.      (minute (string-to-int (aref time 1)))
  207.      (second (string-to-int (aref time 2)))
  208.      (timezone (or timezone local))
  209.      (diff   (- (htz:zone-to-hour timezone)
  210.             (htz:zone-to-hour local)))
  211.      (fixed    (htz:time-fix year month day
  212.                     (+ hour diff) minute second)))
  213.     (htz:date-make-unix
  214.      (aref fixed 0) (aref fixed 1) (aref fixed 2)
  215.      (htz:time-make-string (aref fixed 3) (aref fixed 4) (aref fixed 5))
  216.      timezone)))
  217.  
  218. (defun htz:span-in-days (start-date end-date)
  219.   "Return span in days between START-DATE and END-DATE strings.
  220. See 'htz:date-parse' for a list of acceptable date formats."
  221.   (require 'calendar)
  222.   (let* ((parsed-current-date (htz:date-parse (current-time-string)))
  223.      (htz-start-date (htz:date-parse start-date parsed-current-date))
  224.      (htz-end-date   (htz:date-parse end-date parsed-current-date))
  225.      (cal-start-date
  226.       (list (string-to-int (aref htz-start-date 1))   ;; month
  227.         (string-to-int (aref htz-start-date 2))   ;; day
  228.         (string-to-int (aref htz-start-date 0)))) ;; year
  229.      (cal-end-date
  230.       (list (string-to-int (aref htz-end-date 1))     ;; month
  231.         (string-to-int (aref htz-end-date 2))     ;; day
  232.         (string-to-int (aref htz-end-date 0))))   ;; year
  233.      )
  234.     (- (calendar-absolute-from-julian cal-end-date)
  235.        (calendar-absolute-from-julian cal-start-date))))
  236.  
  237. (defun htz:time-parse (time)
  238.   "Parse TIME (HH:MM:SS) and return a vector [hour minute second]."
  239.   (let ((time (or time ""))
  240.     (hour nil)
  241.     (minute nil)
  242.     (second nil))
  243.     (cond ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)\\'" time)
  244.        ;; HH:MM:SS
  245.        (setq hour 1 minute 2 second 3))
  246.       ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\'" time)
  247.        ;; HH:MM
  248.        (setq hour 1 minute 2 second nil)))
  249.     ;; Return [hour minute second]
  250.     (vector
  251.      (if hour
  252.      (substring time (match-beginning hour) (match-end hour)) "0")
  253.      (if minute
  254.      (substring time (match-beginning minute) (match-end minute)) "0")
  255.      (if second
  256.      (substring time (match-beginning second) (match-end second)) "0"))))
  257.  
  258. ;;; ************************************************************************
  259. ;;; Private functions
  260. ;;; ************************************************************************
  261.  
  262. (defun htz:date-make-arpa (year month day time &optional timezone)
  263.   "Make arpanet standard date string from YEAR, MONTH, DAY, and TIME.
  264. Optional argument TIMEZONE specifies a time zone."
  265.   (format "%02d %s %02d %s%s"
  266.       day
  267.       (capitalize (car (rassq month htz:months-assoc)))
  268.       (- year (* (/ year 100) 100))    ;1990 -> 90
  269.       time
  270.       (if timezone (concat " " timezone) "")
  271.       ))
  272.  
  273. (defun htz:date-make-unix (year month day time &optional timezone)
  274.   "Approximate Unix date format from YEAR, MONTH, DAY, and TIME.
  275. Optional argument TIMEZONE specifies a time zone."
  276.   (format "%s %02d %s%s %04d"
  277.       (capitalize (car (rassq month htz:months-assoc)))
  278.       day time (if timezone (concat " " timezone) "") year))
  279.  
  280. (defun htz:date-make-sortable (year month day time)
  281.   "Make sortable date string from YEAR, MONTH, DAY, and TIME."
  282.   (format "%04d%02d%02d:%s" year month day time))
  283.  
  284. (defun htz:last-day-of-month (month year)
  285.   "The last day in MONTH during YEAR."
  286.   (if (and (= month 2) (htz:leap-year-p year))
  287.       29
  288.     (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
  289.  
  290. (defun htz:leap-year-p (year)
  291.   "Returns t if YEAR is a Gregorian leap year."
  292.   (or (and (zerop  (% year 4))
  293.        (not (zerop (% year 100))))
  294.       (zerop (% year 400))))
  295.  
  296. (defun htz:time-fix (year month day hour minute second)
  297.   "Fix date and time."
  298.   (cond ((<= 24 hour)            ; 24 -> 00
  299.      (setq hour (- hour 24))
  300.      (setq day  (1+ day))
  301.      (if (< (htz:last-day-of-month month year) day)
  302.          (progn
  303.            (setq month (1+ month))
  304.            (setq day 1)
  305.            (if (< 12 month)
  306.            (progn
  307.              (setq month 1)
  308.              (setq year (1+ year)))))))
  309.     ((> 0 hour)
  310.      (setq hour (+ hour 24))
  311.      (setq day  (1- day))
  312.      (if (> 1 day)
  313.          (progn
  314.            (setq month (1- month))
  315.            (if (> 1 month)
  316.            (progn
  317.              (setq month 12)
  318.              (setq year (1- year))))
  319.            (setq day (htz:last-day-of-month month year))))))
  320.   (vector year month day hour minute second))
  321.  
  322. ;; Partly copied from Calendar program by Edward M. Reingold.
  323. (defun htz:time-make-string (hour minute second)
  324.   "Make time string from HOUR, MINUTE, and SECOND."
  325.   (format "%02d:%02d:%02d" hour minute second))
  326.  
  327. (defun htz:zone-to-hour (timezone)
  328.   "Translate TIMEZONE (in zone name or integer) to integer hour."
  329.   (if timezone
  330.       (progn
  331.     (setq timezone
  332.           (or (cdr (assoc (upcase timezone)
  333.                   htz:world-timezones))
  334.           (and (fboundp 'current-time-zone)
  335.                (if (listp (current-time-zone))
  336.                (car (cdr (current-time-zone)))
  337.              (current-time-zone)))
  338.           timezone))
  339.     (if (stringp timezone)
  340.         (setq timezone (string-to-int timezone)))
  341.     (/ timezone 100))
  342.     (error "(htz:zone-to-hour): Nil timezone sent as argument")))
  343.  
  344.  
  345. ;;; ************************************************************************
  346. ;;; Private variables
  347. ;;; ************************************************************************
  348.  
  349. (defvar htz:local
  350.   (or (getenv "TZ") (getenv "TIMEZONE")
  351.       (hypb:call-process-p
  352.        "date" nil '(if (re-search-backward
  353.             " \\([-+a-zA-Z0-9]+\\) [0-9]+$" nil t)
  354.                (buffer-substring (match-beginning 1) (match-end 1)))))
  355.   "Holds string giving the timezone for the local machine.")
  356.  
  357. (defvar htz:world-timezones
  358.   '(("PST" .  -800)
  359.     ("PDT" .  -700)
  360.     ("MST" .  -700)
  361.     ("MDT" .  -600)
  362.     ("CST" .  -600)
  363.     ("CDT" .  -500)
  364.     ("EST" .  -500)
  365.     ("EDT" .  -400)
  366.     ("AST" .  -400)            ;by <clamen@CS.CMU.EDU>
  367.     ("NST" .  -330)            ;by <clamen@CS.CMU.EDU>
  368.     ("GMT" .  +000)
  369.     ("BST" .  +100)
  370.     ("MET" .  +100)
  371.     ("EET" .  +200)
  372.     ("JST" .  +900)
  373.     ("GMT+1"  .  +100) ("GMT+2"  .  +200) ("GMT+3"  .  +300)
  374.     ("GMT+4"  .  +400) ("GMT+5"  .  +500) ("GMT+6"  .  +600)
  375.     ("GMT+7"  .  +700) ("GMT+8"  .  +800) ("GMT+9"  .  +900)
  376.     ("GMT+10" . +1000) ("GMT+11" . +1100) ("GMT+12" . +1200) ("GMT+13" . +1300)
  377.     ("GMT-1"  .  -100) ("GMT-2"  .  -200) ("GMT-3"  .  -300)
  378.     ("GMT-4"  .  -400) ("GMT-5"  .  -500) ("GMT-6"  .  -600)
  379.     ("GMT-7"  .  -700) ("GMT-8"  .  -800) ("GMT-9"  .  -900)
  380.     ("GMT-10" . -1000) ("GMT-11" . -1100) ("GMT-12" . -1200))
  381.   "*Time differentials of timezone from GMT in +-HHMM form.
  382. This list is obsolescent, and is present only for backwards compatibility,
  383. because time zone names are ambiguous in practice.
  384. Use `current-time-zone' instead.")
  385.  
  386. (defvar htz:months-assoc
  387.   '(("JAN" .  1)("FEB" .  2)("MAR" .  3)
  388.     ("APR" .  4)("MAY" .  5)("JUN" .  6)
  389.     ("JUL" .  7)("AUG" .  8)("SEP" .  9)
  390.     ("OCT" . 10)("NOV" . 11)("DEC" . 12))
  391.   "Alist of first three letters of a month and its numerical representation.")
  392.  
  393.  
  394. (provide 'htz)
  395.